package com.kgc.scd.service.impl;

import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.service.CollectWebService;
import com.kgc.scd.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * Created On : 2022/11/14.
 * <p>
 * Author : yantao
 * <p>
 * Description: CollectWebServiceImpl
 */
@Slf4j
@Service
public class CollectWebServiceImpl implements CollectWebService {
    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * @author : yantao
     * @date   : 2022/11/14
     * @param  : [userCollectLimitClickPrefix, userId, goodsId]
     * @return : boolean
     * @description : 限制收藏短时间次数
     */
    @Override
    public boolean testIsExceedNum(String userCollectLimitClickPrefix,String userId) {

        if (redisUtils.checkFreq(userCollectLimitClickPrefix + ":" + userId,userCollectConfig.userCollectClickNum,userCollectConfig.userCollectClickTime)){
            log.info("****** 根据用户编号：{}收藏商品 ******",userId);

            return false;
        }

        return true;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId]
     * @return : boolean
     * @description : 是否到达上限
     */
    @Override
    public boolean isExceedLimit(String userId) {
        log.info("****** 根据用户编号：{}判断是否收藏的物品超过上限 ******",userId);

        if (Integer.parseInt(redisUtils.get(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId).toString()) >userCollectConfig.getUserCollectExceedLimitNum()){

            return true;
        }
        return false;
    }


    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId]
     * @return : boolean
     * @description : 是否收藏过
     */
    @Override
    public boolean isAlreadyCollected(String userId) {
        log.info("****** 根据用户编号：{}判断是否收藏过物品 ******",userId);
        //判断redis是否存在键值对有就是收藏,没有就是没有收藏
        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId))){
            redisUtils.set(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId,0);
            return false;
        }
        return true;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId]
     * @return : void
     * @description : 收藏加一
     */
    @Override
    public void doCollectRedis(String userId) {
        log.info("****** 根据用户编号：{}藏过物品数量加一 ******",userId);
        redisUtils.incr(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId,1);
    }


    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId]
     * @return : void
     * @description : 取消收藏减一
     */
    @Override
    public void cancelCollectionRedis(String userId) {
        log.info("****** 根据用户编号：{}收藏过物品减一 ******",userId);
        redisUtils.decr(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId,1);

        //不能小于0
        if (Integer.parseInt(redisUtils.get(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId).toString()) < 0){

            redisUtils.set(userCollectConfig.getUserCollectExceedLimitPrefix()+":"+userId,0);
        }
    }


}
